/**
 * Copyright (c) 2006-2013 Las Venturas Mineground
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program.
 * If not, see <http://www.gnu.org/licenses/>.
 */

package org.mineground.features.communication;

import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.mineground.Feature;
import org.mineground.events.EventListener;
import org.mineground.events.EventListenerRegistrar;
import org.mineground.events.EventPriority;

/**
 * Administrators on Las Venturas Mineground have the ability to privately talk to each other by
 * prefixing their chat messages with an '@'. The chat message will be cancelled and therefore won't
 * show up for anyone, after which we send a formatted messages to all in-game administrators.
 */
public class AdministratorChat extends EventListenerRegistrar implements Feature {
    private static final String MessagePrefix = "@";
    
    /**
     * Distributes the administrator message sent by the player to all in-game administrators, with
     * the exception of the player itself in case they are an administrator.
     * 
     * @param player The player who sent this message to the chatbox.
     * @param message The message they sent, including the message prefix.
     */
    private void distributeAdministratorChatMessage(Player player, String message) {
        // TODO: Where is the Player Manager?
        // TODO: Re-distribute the message to administrators.
    }
    
    /**
     * Listen to chat events issued by the Bukkit server and check whether they start with the
     * message prefix for administrator chat. If so, we have to do distribution ourselves.
     * 
     * @param event The asynchronous chat event we'll listen to.
     */
    @EventListener(EventPriority.HIGHEST)
    public void onChatMessage(AsyncPlayerChatEvent event) {
        String message = event.getMessage();
        if (!message.startsWith(AdministratorChat.MessagePrefix))
            return;
        
        distributeAdministratorChatMessage(event.getPlayer(), message);
        event.setCancelled(true);
    }
}
